//
//  CutComponent.cpp
//  SchoolLunchFood
//
//  Created by luotianqiang1 on 16/10/25.
//
//

#include "CutComponent.h"
#include "SoundPlayer.h"
const string CutComponent::move_to_target = "move_to_target";
 const string CutComponent::cut_count = "cut_count";
 const string CutComponent::cut_end = "cut_end";

IMPLEMENT_COCOS2DX_CLASS(CutComponent);
void CutComponent::setCutPoints(const vector<Vec2>& temp){
    cutPoints = temp;
}

void  CutComponent::setTarget(LQPolygon* l){
    touchMove->setTarget(l);
}

bool CutComponent::init(){
    if(LQComponent::init()) {
        cutCount = 0;

        linerComonent = TouchLineComonent::create();
        linerComonent->setSwallowTouches(false);
        addComponent(linerComonent);
        
        touchMove = TouchMoveComponent::create();
        touchMove->setSwallowTouches(false);
        touchMove->setBrintTop(false);
        touchMove->setMoveType(MoveComponentType::kTypeBack);
        touchMove->addListners(ComponentTouchMove, [this](LQComponent* lc,OperateListner* lis){
            if(touchMove->checkInlimit()){
                touchMove->setEnabled(false);
                  this->dispatcherEvent(move_to_target);
                   auto inner = getOwner()->getChildByName("inner");
                if(cutPoints.size()>=2&&inner){
                 
                    getOwner()->setCascadeOpacityEnabled(false);
                     getOwner()->setOpacity(0);
                    inner->setVisible(true);
                    
                    getOwner()->runAction(Sequence::create(MoveTo::create(0.05, cutPoints.at(0)),CallFunc::create([=](){
                        prePos = cutPoints.at(0);
                        linerComonent->setDirection(cutPoints.at(0)-cutPoints.at(1));
                        inner->runAction(RepeatForever::create(Sequence::create(MoveBy::create(0.2, Vec2(0,-40)),MoveBy::create(0.2, Vec2(0,40)), nullptr)));
                        Director::getInstance()->getActionManager()->pauseTarget(inner);
                        linerComonent->addListners(ComponentTouchMove, CC_CALLBACK_2(CutComponent::touchMoveCut, this));
                        auto end = [=](LQComponent*,OperateListner* lis){
                             Director::getInstance()->getActionManager()->pauseTarget(inner);
                        };
                        linerComonent->addListners(ComponentTouchEnd,end);
                        linerComonent->addListners(ComponentTouchCancle,end);
                    }), nullptr));
                }
            }
        });
        addComponent(touchMove);
        return true;
    }
    return false;
}

void  CutComponent::touchMoveCut(LQComponent*,OperateListner* lis){
     auto inner = getOwner()->getChildByName("inner");
    if(cutPoints.size()>=2){
        auto deta = dynAny_cast<Vec2>(lis->getData());
        auto newPos = prePos+deta;
        if(newPos.y<cutPoints.at(0).y){
            prePos = newPos;
            if(deta.y<0){
                Director::getInstance()->getActionManager()->resumeTarget(inner);
                if(prePos.y<cutPoints.at(1).y){
                    SoundPlayer::getInstance()->playEffect("sound/cut.mp3");
                    inner->stopAllActions();
                    prePos.y = cutPoints.at(1).y;
                    cutPoints.erase(cutPoints.begin());
                    cutPoints.erase(cutPoints.begin());
                    cutCount++;
                    _childCompoent->erase("TouchLineComonent");
                    getOwner()->removeComponent(linerComonent);
                    dispatcherEventWithData(cut_count, cutCount);
                    if(cutPoints.size()>=2){
                        inner->setPosition(getOwner()->getContentSize()*.5);
                        inner->runAction(RepeatForever::create(Sequence::create(MoveBy::create(0.2, Vec2(0,-40)),MoveBy::create(0.2, Vec2(0,40)), nullptr)));
                        Director::getInstance()->getActionManager()->pauseTarget(inner);
                        getOwner()->runAction(Sequence::create(MoveTo::create(0.2, cutPoints.at(0)),CallFunc::create([=](){
                            prePos = cutPoints.at(0);
                            linerComonent = TouchLineComonent::create();
                            linerComonent->setSwallowTouches(false);
                            this->addComponent(linerComonent);
                            linerComonent->setDirection(cutPoints.at(0)-cutPoints.at(1));
                            inner->runAction(RepeatForever::create(Sequence::create(MoveBy::create(0.2, Vec2(0,-40)),MoveBy::create(0.2, Vec2(0,40)), nullptr)));
                            Director::getInstance()->getActionManager()->pauseTarget(inner);
                            linerComonent->addListners(ComponentTouchMove, CC_CALLBACK_2(CutComponent::touchMoveCut, this));
                            auto end = [=](LQComponent*,OperateListner* lis){
                                Director::getInstance()->getActionManager()->pauseTarget(inner);
                            };
                            linerComonent->addListners(ComponentTouchEnd,end);
                            linerComonent->addListners(ComponentTouchCancle,end);
                        }), nullptr));
                    }else {
                         dispatcherEvent(cut_end);
                    }
                }
            }
            else
                Director::getInstance()->getActionManager()->pauseTarget(inner);
            getOwner()->setPosition(prePos);
        }else {
             Director::getInstance()->getActionManager()->pauseTarget(inner);
        }
    }
}